% simulation of ddot(x)=-k sign(x)
params=struct();
tfinal=40;   
params.k=@(t)1;
initial=[2;2];
[d0,s0]=rhs(0,initial,params);


for k=[1/4 1/2 1 2 4 6]
    params.k=@(t)k;
    params.step=0.01;
    [t,x]=rk4(@(t,x)rhs(t,x,params),0,initial,200,params.step);
    s=repmat(s0,length(t),1);
    for i=1:length(t)
        [di,si]=rhs(t(i),x(i,:)',params);
        s(i)=si;
    end

    figure(1);hold on
    plot(x(:,1),x(:,2),"DisplayName",sprintf("k=%g",params.k(0)))
    xlabel("x")
    ylabel("v")
    axis equal
    grid on
    
end
legend;

function [dxdt,s]=rhs(t,states,p)
    s=struct();
    x=states(1);
    v=states(2);
    s.u=-p.k(t)*sign(x);
    dxdt=[v;s.u];
end